﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.DSJCYD94;
using JM.JCY.Proxy;
using System.Data;
using System.Linq;

namespace JM.JCY.SharePointComponent.LawArchives.ClueDetails
{
    public partial class ClueDetailsUserControl : UserControl
    {
        T_AJBSGXDataSet.T_AJBSGXDataTable AJBSGXData;
        T_DMDataSet.T_DMDataTable DMDT;
        string N_BH;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 N_BH = Request.QueryString["N_BH"].ToString();
                this.lbBH.Text = N_BH;
                DMDT = GetDMData();
                var DT = GetXSGLByBH(Convert.ToDecimal(N_BH));
                BindData(DT);
                AJBSGXData = GetAJBSGXDataByBH(Convert.ToDecimal(N_BH));

                lbBATS.Text = AJBSGXData.Count > 0 ? AJBSGXData[0].N_BATS.ToString() : "0";
                DataRow[] bJBRRows = GetBJBRByBH(Convert.ToDecimal(N_BH)).Select();
                dlBJBR.DataSource = bJBRRows;
                dlBJBR.DataBind();
                if (bJBRRows.Count() > 0)
                {

                    BJBRNoRecord.Visible = false;
                }

                else
                    BJBRNoRecord.Visible = true;

                DataRow[] CLQKRows = GetCLQKByBH(Convert.ToDecimal(N_BH)).Select();
                dlCLQK.DataSource = CLQKRows;
                dlCLQK.DataBind();
                if (CLQKRows.Count() > 0)
                {

                    CLQKNoRecords.Visible = false;
                }

                else
                    CLQKNoRecords.Visible = true;

            }
        }
        public T_AJBSGXDataSet.T_AJBSGXDataTable GetAJBSGXDataByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetAJBSGXDataByBH(BH);
            }
        }
        public void BindData(T_XSGL_JBXSXXDataSet.T_XSGLDataTable DT)
        {
            var row = DT[0];
            this.lbID.Text = string.IsNullOrEmpty(row.C_AH) ? string.Empty : row.C_AH.ToString();
            this.LabelGetDate.Text = row.IsD_SARQNull() ? string.Empty : row.D_SARQ.ToString("yyyy年MM月dd日");
            this.lbPerson.Text = string.IsNullOrEmpty(row.C_XM) ? string.Empty : row.C_XM.ToString();
        }

        protected void dlCLQK_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            T_XSGL_JBXSXXDataSet.T_XSGL_JBCLQKRow row = (T_XSGL_JBXSXXDataSet.T_XSGL_JBCLQKRow)e.Item.DataItem;
            Label lbHandleDate = (Label)e.Item.FindControl("lbHandleDate");
            Label lbHandleResult = (Label)e.Item.FindControl("lbHandleResult");
            Label lbHandleExplanation = (Label)e.Item.FindControl("lbHandleExplanation");
            lbHandleDate.Text = row.IsD_CLRQNull() ? string.Empty : row.D_CLRQ.ToString("yyyy年MM月dd日");
            //处理结论
             var CLJL = from dm in DMDT
                       where dm.N_BXH==680&&!row.IsN_CLJLNull() && dm.N_DM == row.N_CLJL
                       select dm.C_MC;
             lbHandleResult.Text = CLJL.Count() > 0 ? CLJL.First() : string.Empty;
            //处理说明 
             if (row.IsI_XSCLSMNull())
             {
                 lbHandleExplanation.Text = string.Empty;
             }
             else
             { 
               var DT=GetDWBZDData(row.I_XSCLSM, Convert.ToDecimal(N_BH));
               if (DT.Count > 0)
               {
                   byte[] by = DT[0].I_NR;
                   lbHandleExplanation.Text = System.Text.Encoding.ASCII.GetString(by);
               }
               else
               {
                   lbHandleExplanation.Text = string.Empty;
               }
             }

        }
        protected void dlBJBR_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            T_XSGL_JBXSXXDataSet.T_XSGL_BJBRXXRow row = (T_XSGL_JBXSXXDataSet.T_XSGL_BJBRXXRow)e.Item.DataItem;
            Label lbName = (Label)e.Item.FindControl("lbName");
            Label lbGender = (Label)e.Item.FindControl("lbGender");
            Label lbBusiness = (Label)e.Item.FindControl("lbBusiness");
            Label lbWorkLevel = (Label)e.Item.FindControl("lbWorkLevel");
            if(string.IsNullOrEmpty(row.C_XM))
            {
            lbName.Text = string.IsNullOrEmpty(row.C_DWMC) ? string.Empty : row.C_DWMC.ToString();
            }
            else
            {
            lbName.Text =row.C_XM;
            }
            // 单位类别
            var DWLB = from dm in DMDT
                       where dm.N_BXH==1014&&!row.IsN_DWXZNull() && dm.N_DM == row.N_DWXZ
                       select dm.C_MC;
            if (row.IsN_XBNull())
            {
                lbGender.Text = DWLB.Count() > 0 ? DWLB.First() : string.Empty;
            }
            else
            {
                lbGender.Text = row.N_XB == 1 ? "男" : "女";
            }
            //身份 
            var SF = from dm in DMDT
                     where dm.N_BXH == 48 & !row.IsN_SFNull() && dm.N_DM == row.N_SF
                       select dm.C_MC;
            //所属行业  
            var SSHY = from dm in DMDT
                       where dm.N_BXH == 1009 & !row.IsN_SSHYNull() && dm.N_DM == row.N_SSHY
                       select dm.C_MC;
            if(row.IsN_SFNull())
            {
                lbBusiness.Text = SSHY.Count() > 0 ? SSHY.First() : string.Empty;
            }
            else
            {
                lbBusiness.Text = SF.Count() > 0 ? SF.First() : string.Empty;
            }
            //职级 
            var ZJ = from dm in DMDT
                     where dm.N_BXH == 13 & !row.IsN_ZJNull() && dm.N_DM == row.N_ZJ
                       select dm.C_MC;
            //单位级别 1012
            var DWJB = from dm in DMDT
                       where dm.N_BXH == 1012 & !row.IsN_DWJBNull() && dm.N_DM == row.N_DWJB
                     select dm.C_MC;
            if(row.IsN_ZJNull())
            {
                lbWorkLevel.Text = DWJB.Count() > 0 ? DWJB.First() : string.Empty;
            }
            else
            {
                lbWorkLevel.Text = ZJ.Count() > 0 ? ZJ.First() : string.Empty;
            }
            
            
           
        }

        //Method to get the data in table T_DM
        public T_DMDataSet.T_DMDataTable GetDMData()
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetDMData();
            }
        }
        public T_XSGL_JBXSXXDataSet.T_XSGLDataTable GetXSGLByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetXSGLDataByBH(BH);
            }
        }

        public T_XSGL_JBXSXXDataSet.T_XSGL_BJBRXXDataTable GetBJBRByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetXSGL_BJBRByBH(BH);
            }
        }

        public T_XSGL_JBXSXXDataSet.T_XSGL_JBCLQKDataTable GetCLQKByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetXSGL_JBCLQKByBH(BH);
            }
        }

        public T_XSGL_JBXSXXDataSet.T_DWBZDDataTable GetDWBZDData(decimal id, decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetDWBZDData(id,BH);
            }
        }
    }
}
